package com.jiangyao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiangyao.common.constants.CacheConstants;
import com.jiangyao.common.exception.ServerException;
import com.jiangyao.component.EmailComponent;
import com.jiangyao.component.RedisComponent;
import com.jiangyao.component.SMSComponent;
import com.jiangyao.dao.*;
import com.jiangyao.model.dto.EditDeliverDTO;
import com.jiangyao.model.dto.ResumeSendDTO;
import com.jiangyao.model.dto.backend.EditStatusDTO;
import com.jiangyao.model.entity.*;
import com.jiangyao.model.vo.SendJobVo;
import com.jiangyao.model.vo.UserSessionVO;
import com.jiangyao.service.ResumeSendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 简历投递（求职） 服务实现类
 * </p>
 *
 * @author zhoushuangxiang
 * @since 2020-08-12
 */
@Service
public class ResumeSendServiceImpl extends BaseServiceImpl<IResumeSendDao, ResumeSend> implements ResumeSendService {

    @Autowired
    private IResumeSendDao iResumeSendDao;

    @Autowired
    private IInterviewInvitationDao iInterviewInvitationDao;

    @Autowired
    private IJobDao iJobDao;

    @Autowired
    private IAdminUserDao iAdminUserDao;

    @Autowired
    private IUserDao iUserDao;

    @Autowired
    private IOrgInfoDao iOrgInfoDao;

    @Autowired
    private SMSComponent smsComponent;

    @Autowired
    private RedisComponent redisComponent;

    @Autowired
    private EmailComponent emailComponent;

    @Override
    @Transactional
    public ResumeSend send(ResumeSend resumeSend) throws ServerException{
        UserSessionVO apiLoginSession = getApiLoginSession();
        resumeSend.setUserId(apiLoginSession.getUserId());
        QueryWrapper<ResumeSend> resumeSendQueryWrapper = new QueryWrapper<>();
        resumeSendQueryWrapper.eq("org_id",resumeSend.getOrgId()).eq("user_id",resumeSend.getUserId())
                .eq("job_id",resumeSend.getJobId());
        ResumeSend resumeSend1 = iResumeSendDao.selectOne(resumeSendQueryWrapper);
        if(resumeSend1 !=null){
            throw new ServerException(500,"请不要重复提交简历");
        }
        iResumeSendDao.insert(resumeSend);
        return resumeSend;
    }

    @Override
    public Boolean sendMessage(ResumeSendDTO resumeSendDTO) throws ServerException{
        resumeSendDTO.setUserId(getLoginApiUserId());//发布的时候上线
        //resumeSendDTO.setUserId(12369);

        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("user_id",resumeSendDTO.getUserId());
        userQueryWrapper.last("limit 1");
        User user = iUserDao.selectOne(userQueryWrapper);

        SendJobVo sendJobVo=iJobDao.querySendJob(resumeSendDTO);

        //发送短信
        smsComponent.sendResume(sendJobVo.getPhone(),sendJobVo.getOrgName(),user.getUserName(),sendJobVo.getJobName());
        String s1 = sendJobVo.getPhone() + "+" + sendJobVo.getOrgName() + "+" + user.getUserName() + "+" + sendJobVo.getJobName();
        redisComponent.set(CacheConstants.CACHE_KEY_TOKEN_API_VERIFICATION + sendJobVo.getPhone(), s1, CacheConstants.CACHE_TIME_15_MINUTE);
        emailComponent.send(sendJobVo.getEmail(),sendJobVo.getOrgName(),user.getUserName(),sendJobVo.getJobName());
        return true;

    }


    @Override
    @Transactional
    public void editStatus(EditDeliverDTO editDeliverDTO) throws ServerException {
        try {
            UserSessionVO apiLoginSession = getApiLoginSession();
            editDeliverDTO.setUserId(apiLoginSession.getUserId());
            iResumeSendDao.editStatus(editDeliverDTO);
            iInterviewInvitationDao.editStatus(editDeliverDTO);
            //修改面试详情
        } catch (Exception e) {
            throw new ServerException(500,"修改失败");
        }
    }

    @Override
    public void editStatus(EditStatusDTO editStatusDTO) {
        iResumeSendDao.editStatusTpye(editStatusDTO);
    }
}
